.PHONY:all clean mk_crt mk_main ec help

M = 32

C_FLAGS = -fno-stack-protector
C_FLAGS += -fno-builtin #close inter-replace by gcc, such as print -> puts
C_FLAGS += -ggdb
C_FLAGS += -Wall
C_FLAGS += -O0

ifeq (32,$(M))
	C_FLAGS += -m32
	C_FLAGS += -DM32
else
	C_FLAGS += -m64
	C_FLAGS += -DM64
endif



LD_FLAGS = -nostartfiles 
LD_FLAGS += -no-stdlib #do not use any lib and start-file(such as bengin.o) from gcc, it include -nostartfile option
LD_FLAGS += -O0
ifeq (32,$(M))
	LD_FLAGS += -m elf_i386
else
endif

all:mk_main
	
mk_main:mk_crt 
	gcc $(C_FLAGS) -c main.c -o main.o
	ld $(LD_FLAGS) entry.o main.o libmini_crt.a -e mini_crt_entry -o e
	@rm -f ./*.o


mk_crt:
	gcc $(C_FLAGS) -c -o entry.o entry.c
	gcc $(C_FLAGS) -c -o syscall.o syscall.S
	gcc $(C_FLAGS) -c -o mini_crt.o mini_crt.c
	ar -rs libmini_crt.a mini_crt.o syscall.o

clean:
	rm -f ./*.o ./e ./*.a ./core ./test.txt

ec:
	ulimit -c unlimited
	@echo "usage:gdb ./e ./core"

help:
	@echo "make M=64   -> for 64bit exe-file"
	@echo "make M=32   -> for 32bit exe-file"
	@echo "make        -> for 32bit exe-file"
	@echo "make ec     -> enable core dump."
